import java.util.HashMap;
import java.util.Map;

/**
 * @Author ZhangCuirong
 * @Date 2025/9/4 14:39
 * @description:
 */
public class Solution523 {
    public boolean checkSubarraySum(int[] nums, int k) {
        if (nums.length < 2) {
            return false;
        }
        int[] prefixSum = new int[nums.length + 1];
        for (int i = 0; i < nums.length; i++) {
            prefixSum[i + 1] = prefixSum[i] + nums[i];
        }
        Map<Integer, Integer> map = new HashMap<>();
        map.put(prefixSum[0] % k, 0);
        for (int end = 1; end <= nums.length; end++) {
            if (!map.containsKey(prefixSum[end] % k)) {
                map.put(prefixSum[end] % k, end);
            } else {
                if (end - map.get(prefixSum[end] % k) >= 2) {
                    return true;
                }
            }
        }
        return false;
    }

    public static void main(String[] args) {
        Solution523 solution = new Solution523();
        System.out.println(solution.checkSubarraySum(new int[]{23, 2, 4, 6, 7}, 6));
        System.out.println(solution.checkSubarraySum(new int[]{23, 2, 6, 4, 7}, 6));
        System.out.println(solution.checkSubarraySum(new int[]{23, 2, 6, 4, 7}, 13));
        System.out.println(solution.checkSubarraySum(new int[]{5, 0, 0, 0}, 3));
        System.out.println(solution.checkSubarraySum(new int[]{0, 0}, 1));
        System.out.println(solution.checkSubarraySum(new int[]{1, 1}, 2));
    }
}
